﻿
using System;
using UnityEngine;
using UnityEngine.U2D;
using WooEngine.Common;
using WooEngine.Utility.Logger;

namespace WooEngine.ConfigWork
{
    public class LangManager : SingletonBehaviour<LangManager>
    {
        public ConfigFileData m_data;
        private SpriteAtlas m_artFontAtlas;
        private int m_changeLangCount = 0;

        private int m_lang = -1;

        private bool m_isHasArtFont = true;

        public int GetLang()
        {
            return m_lang;
        }

        public void Clear()
        {
            this.m_data = null;
        }

        public void InitLang(int langValue, Action callBack)
        {
            SetLang(langValue, callBack);
        }

        public void SetLang(int langValue, Action callBack)
        {
            WooLogger.InfoAppendString("current lang :",m_lang, ". Set Lang:", langValue );
            if (m_lang == langValue)
            {
                if (callBack != null)
                {
                    callBack();
                }
                return;
            }
            m_lang = langValue;
            WooLogger.InfoAppendString("current lang :", m_lang);
            this.changeLang(callBack);
        }

        private void changeLang(Action callBack)
        {
            m_changeLangCount = m_isHasArtFont?2:1; 
            string langFileName = StringUtils.AppendString( "game_lang_", m_lang);
            // 加载成功后的回调函数
            void OnFinishText(TextAsset textAsset, object data)
            {
                if (textAsset != null)
                {
                    // Logger.Log(" langFileName: " + langFileName+"|"+ textAsset);
                    // 2 添加数据到管理器
                    ConfigManager.Instance.AddFile(textAsset, langFileName);
                    // 3 将语言数据添加到语言管理器
                    this.m_data = ConfigManager.Instance.GetConfigFileData(langFileName);

                    Res.ResourceManager.Instance.ReleaseAsset(textAsset);

                    loadFinish(callBack);
                }
            }
            Res.ResourceManager.Instance.LoadAssetAsync<TextAsset>(langFileName, OnFinishText);

            if (m_isHasArtFont)
            {
                string artFontFileName = StringUtils.AppendString("artfont_", m_lang, "_atlas");
                if (m_artFontAtlas != null)
                {
                    Res.ResourceManager.Instance.ReleaseAsset(m_artFontAtlas);
                    m_artFontAtlas = null;
                }
                // 加载成功后的回调函数
                void OnFinishArtFont(SpriteAtlas artFontAtlas, object data)
                {
                    m_artFontAtlas = artFontAtlas;
                    m_artFontAtlas.name = artFontFileName;

                    loadFinish(callBack);
                }
                Res.ResourceManager.Instance.LoadAssetAsync<SpriteAtlas>(artFontFileName, OnFinishArtFont);
            }
        }

        private void loadFinish(Action callBack)
        {
            m_changeLangCount--;
            if (m_changeLangCount > 0)
                return;
            if (callBack != null)
            {
                callBack();
            }
        }

        public string GetLocalString(string langID)
        {
            string str = "";
            if (this.m_data == null)
            {
                WooLogger.WarnAppendString("LangManager is not init localString langID  ", langID, "=", this.m_data);
                return langID;
            }
            if (this.m_data.GetValue(langID) == null)
            {
                WooLogger.WarnAppendString("LangManager haven't str By langID  ", langID);
                str = langID;
            }
            else
            {
                str = this.m_data.GetValue(langID).str;
                if (str == null)
                {
                    WooLogger.WarnAppendString("LangManager haven't str By langID  ", langID);
                    str = langID;
                }
            }
            return str.Replace("\\\"", "\"");
        }

        public Sprite GetLocalArtFontString(string name)
        {
            if (m_artFontAtlas == null)
            {
                WooLogger.ErrorAppendString("GetLocalArtFontString m_artFontAtlas == null : name = ", name);
                return null;
            }
            return m_artFontAtlas.GetSprite(name);
        }

        public void GetLocalArtBigImg(string name, Action<Sprite, object> callBack, object userdata = null)
        {
            Res.ResourceManager.Instance.LoadAssetAsync<Sprite>(StringUtils.AppendString(name,"_lang_", m_lang), callBack, userdata);
        }

    }
}